﻿using DapperExtensions;
using Interface.Core.Controllers;
using Interface.Core.Utility;
using Interface.Mvc.Areas.Scrm.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Interface.Utility.Helper;

namespace Interface.Mvc.Areas.Scrm.Controllers
{
    public class BabyController : CoreController
    {
        /// <summary>
        /// 根据会员编号获取宝宝信息
        /// url: /scrm/baby/query
        /// params: memberid
        /// </summary>
        [HttpGet]
        public string Query(int memberid)
        {
            var predicate = Predicates.Field<MemberBaby>(f => f.MemberID, Operator.Eq, memberid);

            var list = db.Scrm.GetList<MemberBaby>(predicate);

            return ResultUtility.SuccessResult(data: list);
        }

        /// <summary>
        /// 根据编号获取宝宝信息
        /// url: /scrm/baby/get
        /// params: id
        /// </summary>
        [HttpGet]
        public string Get(int id)
        {
            var predicate = Predicates.Field<MemberBaby>(f => f.ID, Operator.Eq, id);

            var entity = db.Scrm.Get<MemberBaby>(predicate);

            if (entity != null)
            {
                return ResultUtility.SuccessResult(data: entity);
            }

            return ResultUtility.FailureResult();
        }

        /// <summary>
        /// 添加宝宝信息
        /// url: /scrm/baby/insert
        /// params: memberid, name, gender, nickname, birthday, remark
        /// </summary>
        [HttpPost]
        public string Insert()
        {
            var entity = new MemberBaby().FormFill();

            entity.IncludeDate = DateTime.Now;

            int id = db.Scrm.Insert(entity);

            if (id > 0)
            {
                return ResultUtility.SuccessResult();
            }

            return ResultUtility.FailureResult();
        }

        /// <summary>
        /// 修改宝宝信息
        /// url: /scrm/baby/update
        /// params: id, memberid, name, gender, nickname, birthday, remark
        /// </summary>
        [HttpPost]
        public string Update()
        {
            var entity = new MemberBaby().FormFill();

            entity.IncludeDate = DateTime.Now;

            if (db.Scrm.Update(entity))
            {
                return ResultUtility.SuccessResult();
            }

            return ResultUtility.FailureResult();
        }

        /// <summary>
        /// 删除宝宝信息
        /// url: /scrm/baby/delete
        /// params: id
        /// </summary>
        [HttpPost]
        public string Delete(int id)
        {
            var predicate = Predicates.Field<MemberBaby>(f => f.ID, Operator.Eq, id);

            if (db.Scrm.Delete<MemberBaby>(predicate))
            {
                return ResultUtility.SuccessResult();
            }

            return ResultUtility.FailureResult();
        }

        /// <summary>
        /// 根据会员编号删除宝宝信息
        /// url: /scrm/baby/deletebymemberid
        /// params: memberid
        /// </summary>
        [HttpPost]
        public string DeleteByMemberID(int memberid)
        {
            var predicate = Predicates.Field<MemberBaby>(f => f.MemberID, Operator.Eq, memberid);

            if (db.Scrm.Delete<MemberBaby>(predicate))
            {
                return ResultUtility.SuccessResult();
            }

            return ResultUtility.FailureResult();
        }
    }
}
